package com.sunlands.zlcx.usercenter.service.status;

import com.sunlands.zlcx.usercenter.constant.LearningStatus;
import com.sunlands.zlcx.usercenter.constant.PublisherStatus;
import com.sunlands.zlcx.usercenter.constant.RedisKeyConst;
import com.sunlands.zlcx.usercenter.constant.YN;
import com.sunlands.zlcx.usercenter.domain.UsersLearnTimingDO;
import com.sunlands.zlcx.usercenter.domain.UsersTargetRelationDO;
import com.sunlands.zlcx.usercenter.exception.CheckParamException;
import com.sunlands.zlcx.usercenter.exception.UserNotLearningException;
import com.sunlands.zlcx.usercenter.service.VipUserServiceImpl;
import com.sunlands.zlcx.usercenter.service.status.common.AbstractStudyStatus;
import com.sunlands.zlcx.usercenter.util.BeanPropertiesUtil;
import com.sunlands.zlcx.usercenter.util.CalendarUtil;
import com.sunlands.zlcx.usercenter.vo.UserMonthLearnVO;
import com.sunlands.zlcx.usercenter.vo.UsersStudyInfoVO;
import com.sunlands.zlcx.usercenter.vo.feign.RoomPublisherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author gaoyanfeng
 * @create 2019-04-28 9:50
 **/
@Component
@Slf4j
public class LianMaiStartLearn extends AbstractStudyStatus implements BeanPropertiesUtil {

    @Autowired
    private VipUserServiceImpl vipUserService;

    @Override
    public void handle(UsersStudyInfoVO studyInfo, UsersTargetRelationDO targetRelation) {
        Long userId = targetRelation.getUserId();
        if (null == studyInfo.getLianMaiId()) {
            throw new CheckParamException("连麦id参数不合法");
        }
            RoomPublisherVO publishRecord = liveVideoService.getPublishRecordId(studyInfo.getLianMaiId()).getData();
            log.info("调用连麦系统接口 publishRecord = {}", publishRecord);

            Integer publisherState = publishRecord.getPublisherState();
            Long publishUserId = publishRecord.getUserId();
            if (PublisherStatus.PUBLISHING.getCode() != publisherState || !publishUserId.equals(userId) ) {
                throw new UserNotLearningException("该用户还未进行连麦 userId = " + studyInfo.getUserId());
            }
            checkTimingPeriod(studyInfo, userId);
            redisTemplate.opsForValue().set(RedisKeyConst.getStudyName(userId), studyInfo.getStudyInfoName());

            Date startTime = new Date();
            usersService.updateStatus(userId, LearningStatus.LIAN_MAI_TIMING.getCode(), startTime, studyInfo.getRoomId(), studyInfo.getLianMaiId());

            Date countTime = CalendarUtil.getDateHoursBefore(startTime, 3);
            UsersLearnTimingDO usersLearnTimingDO = UsersLearnTimingDO
                    .builder()
                    .contentId(studyInfo.getContentId())
                    .status(YN.NO.getCode())
                    .learnName(studyInfo.getStudyInfoName())
                    .targetId(targetRelation.getTargetId())
                    .countTime(countTime)
                    .week(CalendarUtil.getWeek(countTime))
                    .startTime(startTime)
                    .type(UsersLearnTimingDO.TypeEnums.LIAN_MAI.code)
                    .endTime(studyInfo.getEndTime())
                    .userId(studyInfo.getUserId())
                    .build();

            UserMonthLearnVO.FriendLearnInfo learnInfo = usersStudyService.getLearnRankList(userId, 0)
                    .stream()
                    .filter(friendLearnInfo -> friendLearnInfo.getFriendId().equals(userId))
                    .findFirst()
                    .orElse(null);
            log.debug("FriendLearnInfo = {} ", learnInfo);
            usersLearnTimingDO.setStartSort(Objects.nonNull(learnInfo) ? learnInfo.getIndex() : 1);
            usersLearnTimingRepository.save(usersLearnTimingDO);

            vipUserService.speedUpReduce(userId, 5);

    }
}
